package Control;

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.Date;
import Database.*;
import Model.*;

import javax.swing.*;

public class AddProductGUI extends JFrame implements ActionListener 
{
	private JLabel jlDes, jlCost, jlPrice, jlQty, jlReorder, jlRAmount,
			jlSupplier;
	private JTextField jtDes, jtCost, jtPrice, jtQty, jtReorder, jtRAmount,
			jtSupplier;
	private JButton add, cancel;
	private JPanel p1, p2;
	private TableOperations to;
	private static DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
	private Date inDate = null;
	private java.sql.Date sqlDate;
	private ImageIcon icon = new ImageIcon("frameIcon.png");

	public AddProductGUI() 
	{
		setIconImage(icon.getImage());
		setTitle("MDL - Add Product");
		setSize(300, 225);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		to = new TableOperations();
		to.queryProducts();

		p1 = new JPanel();
		p1.setLayout(new GridLayout(8, 2));
		add(p1);

		jlDes = new JLabel("Description");
		p1.add(jlDes);

		jtDes = new JTextField("");
		p1.add(jtDes);

		jlCost = new JLabel("Cost");
		p1.add(jlCost);

		jtCost = new JTextField("");
		p1.add(jtCost);

		jlPrice = new JLabel("Price");
		p1.add(jlPrice);

		jtPrice = new JTextField("");
		p1.add(jtPrice);

		jlQty = new JLabel("Quantity");
		p1.add(jlQty);

		jtQty = new JTextField("");
		p1.add(jtQty);

		jlReorder = new JLabel("Reorder level");
		p1.add(jlReorder);

		jtReorder = new JTextField("");
		p1.add(jtReorder);

		jlRAmount = new JLabel("Reorder Quantity");
		p1.add(jlRAmount);

		jtRAmount = new JTextField("");
		p1.add(jtRAmount);

		jlSupplier = new JLabel("Supplier ID");
		p1.add(jlSupplier);

		jtSupplier = new JTextField("");
		p1.add(jtSupplier);

		p2 = new JPanel(new FlowLayout());
		add(p2, BorderLayout.SOUTH);

		add = new JButton("Add Product");
		add.addActionListener(this);
		p2.add(add);

		cancel = new JButton("Cancel");
		cancel.addActionListener(this);
		p2.add(cancel);

	}

	public void actionPerformed(ActionEvent e) throws NumberFormatException 
	{
		if (e.getSource() == add) 
		{
			try 
			{
				boolean forward = true;
				if (jtDes.getText().equals("") || jtPrice.getText().equals("")
						|| jtQty.getText().equals("") || jtCost.getText().equals("") || jtSupplier.getText().equals("")) 
				{
					JOptionPane.showMessageDialog(null, "Check Blank Fields");
					forward = false;
				}

				else if (Double.parseDouble(jtPrice.getText()) <= 0	|| Double.parseDouble(jtCost.getText()) <= 0
							|| Integer.parseInt(jtQty.getText()) <= 0 || Integer.parseInt(jtSupplier.getText()) <= 0) 
				{
					JOptionPane.showMessageDialog(null,	"Price, Quantity, Cost or Supplier ID cannot be 0 or less and must be a number");
					forward = false;
				}

				if (forward == true) 
				{
					Product p = new Product(jtDes.getText(),
					Double.parseDouble(jtCost.getText()),
					Double.parseDouble(jtPrice.getText()),
					Integer.parseInt(jtQty.getText()),
					Integer.parseInt(jtReorder.getText()),
					Integer.parseInt(jtRAmount.getText()),
					Integer.parseInt(jtSupplier.getText()));
					ProductGUI pg = new ProductGUI(p);
					pg.setVisible(true);
					this.dispose();
				}
			}
			catch (NumberFormatException nfe) 
			{
				JOptionPane.showMessageDialog(null,	"Please ensure all numbers are entered in numerical form e.g. 1 not one");
			}

		}

		else if (e.getSource() == cancel) 
		{
			this.dispose();
		}

	}
}